import { getStorage, setStorage } from "@/utils/storage"

// 获取租户id
export const getTenantId = () => {
  let tenantId = "",
    TENANT_ID = ""
  try {
    tenantId = getStorage({ name: 'tenantId' })
    TENANT_ID = tenantId
      ? tenantId
      : ''
  } catch (error) { }
  return TENANT_ID
}

export const setToken = (val: string) => {
  setStorage({
    name: "access_token",
    content: val
  })
}
// 获取token
export const getToken = () => {
  const token = getStorage({
    name: 'access_token'
  }) || ''
  return token
}

// 根据path找到当前appid，让多个应用后退
export const getAppId = () => {
  const tenantList = getStorage({ name: 'tenantList' })
  // console.log('tenantList', tenantList)
  // console.log(window.location)
  if (!tenantList || !tenantList.length) return ''
  const hasTenantId = tenantList.find((item: any) => item.tenantId === getTenantId())
  if (!hasTenantId) return ''
  const pathname = window.location.pathname
  const data = tenantList.find((item: any) => item.templateFolder && pathname.includes(item.templateFolder))
  if (!data) return ''
  setStorage({
    name: "factoryInfo",
    content: data
  })
  setStorage({
    name: "tenantId",
    content: data.tenantId
  })
  return data.id
}